package com.xizi.lotterySystem.common.pojo;


import cn.hutool.core.lang.Assert;
import com.xizi.lotterySystem.common.exception.errorCode.ErrorCode;
import com.xizi.lotterySystem.common.exception.errorCode.GlobalErrorCodeConstants;
import lombok.Data;

import java.io.Serializable;


//统一结果返回
@Data
public class CommonResult <T> implements Serializable {
    private int code;
    private String msg;
    private T data;


    public CommonResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    public CommonResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public CommonResult() {}

    public CommonResult(ErrorCode errorCode) {
        this.code = errorCode.getCode();
        this.msg = errorCode.getMessage();
    }

    public static <T>CommonResult<T> success(T data){
        CommonResult<T> result = new CommonResult<>(GlobalErrorCodeConstants.SUCCESS);
        result.setData(data);
        return result;
    }

    public static <T>CommonResult<T> fail(ErrorCode errorCode){
        return new CommonResult<>(errorCode);
    }

    public static <T>CommonResult<T> fail(Integer code,String msg){
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code),"错误码为200，调用错了！");
        return new CommonResult<>(code,msg);
    }

}
